package com.meego.common.timer;

import com.meego.common.misc.SafeListIterator;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@SuppressWarnings("unchecked")
public class MultiEvent implements TimedEvent {

    private TimedEvent mEvent;
    private List mListeners;

    public MultiEvent(TimedEvent anEvent) {
        this.mEvent = anEvent;
        this.mListeners = new ArrayList();
    }

    public synchronized void addListener(TimeListener aListener) {
        if (this.mListeners.contains(aListener)) {
            return;
        }
        this.mListeners.add(aListener);
    }

    public synchronized void removeListener(TimeListener aListener) {
        this.mListeners.remove(aListener);
    }

    @Override
    public void doTimedEvent() {
        Iterator i = getListeners();

        while (i.hasNext()) {
            ((TimeListener) i.next()).timeIntervalPassed();
        }
    }

    @Override
    public Date getNextEvent() {
        return this.mEvent.getNextEvent();
    }

    private synchronized Iterator getListeners() {
        return new SafeListIterator(this.mListeners);
    }
}
